package com.crazy.appium.testcases;

import java.io.File;
import java.lang.reflect.Method;

import org.openqa.selenium.By;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Parameters;

import com.crazy.appium.datadriver.ExcelUtil;
import com.crazy.appium.driver.CrazyMobileDriver;
import com.crazy.appium.utils.SendMail;

import io.appium.java_client.MobileBy;
import io.appium.java_client.service.local.AppiumDriverLocalService;
import io.appium.java_client.service.local.AppiumServiceBuilder;
import io.appium.java_client.service.local.flags.ServerArgument;

/**
 * @author 沙陌 qq2879897713
 *
 */
public class TestBase {
	public CrazyMobileDriver driver;
	// AppiumDriverLocalService service;
	String udid;
	String platform;
	boolean loginStatus=false;

	public CrazyMobileDriver getDriver() {
		return driver;
	}

	public String getUdid() {
		return udid;
	}
	public String getPlatform() {
		return platform;
	}

	@Parameters({ "udid", "port", "platform" })
	@BeforeClass
	public void beforeClass(String udid, String port, String platform) throws Exception {
		this.udid = udid;
		this.platform = platform;
		System.out.println(udid + port);
		try {
			driver = new CrazyMobileDriver(platform, udid, port);
		} catch (Exception e) {
			// TODO: handle exception
			SendMail se=new SendMail();
			se.send("driver init failure", "driver init failure-->"+udid+"-->"+e.getMessage());
		}
		driver.implicitlyWaitDefault();
		driver.sleep(10000);

	}

	@AfterClass
	public void afterClass() {
		driver.quit();
		// service.stop();
	}
	
	@DataProvider
	public Object[][] getData(Method method) throws Exception{
		ExcelUtil excel=new ExcelUtil("C:/Users/LXG/Desktop/liecai.xlsx");
        Object[][] result = null;
        if(method.getName().equals("test001_login")){
            result = excel.getTestData("login");
        }else if(method.getName().equals("test004_search")){
        	result = excel.getTestData("search");
        }else{
            result = new Object[][]{new Object[]{3}};
        }
        return result;
	}
	@AfterMethod
	public void backHome(){
		if(platform.equalsIgnoreCase("android")){
			driver.startActivity((String)driver.getCaps().getCapability("appPackage"), (String)driver.getCaps().getCapability("appActivity"),"com.toobei.lcds.activity.MainActivity");
			driver.sleep(6000);
		}else{
			//ios的话需要用到不断返回直到首页元素出现为止
			while(driver.isElementExist(By.xpath("//*[resource-id='com.toobei.lcds:id/mainActivity_RadioGroup']"))){
				driver.findElement(MobileBy.AccessibilityId("back")).click();
			}
		}
//		driver.closeApp();
//		driver.launchApp();
		
		
	}
	


}
